<?php
class CommonController extends Zend_Controller_Action 
{ 
	function init(){
		parent::init();
		$this->registry=Zend_Registry::getInstance();
		$this->view=$this->registry['view'];
		$this->view->baseUrl=BLOG_ROOT_URL;
		$this->view->styleurl=$this->view->baseUrl."/public/styles/default";
		// 站点基本信息
		$this->view->version = $this->registry['version'];
		$this->view->domain = $this->registry['domain'];
		$this->view->sitename = $this->registry['sitename'];
		$this->view->keyword = $this->registry['keyword'];
		$this->view->footword = $this->registry['footword'];
		$this->view->record = $this->registry['record'];
		
    	$use_cache = false;
		//包含header头部  left部 foot部
		if($use_cache){//使用缓存
			// 缓存配置,数据缓存
			$frontendOptions = array(
	        	'lifeTime' => 120, // 两分钟的缓存生命期
	        	'automatic_serialization' => true
	        );
	        $backendOptions = array(
	        	'cache_dir' => "./data/cache"
	        );
	        // 取得一个Zend_Cache_Core 对象
	    	$cache = Zend_Cache::factory('Core','File',$frontendOptions,$backendOptions);
			// 查看一个缓存是否存在:
			if(!$this->view->header_class = $cache->load('index_header_info')) {
			    // 缓存不命中;连接到数据库
				load_model('Class');
				$class = new APclass();
				$this->view->header_class = $class->get_class_index();
			    $cache->save($this->view->header_class, 'index_header_info');
			}
			if(!$this->view->user_info = $cache->load('index_user_info')) {
				load_model('User');
				$user = new User();
				$this->view->user_info = $user->get_user_info(1);
			    $cache->save($this->view->user_info, 'index_user_info');
			}
			if(!$this->view->art_class = $cache->load('index_art_class')) {
				load_model('Class');
				$class = new APclass();
				$this->view->art_class = $class->get_artclass_list();
			    $cache->save($this->view->art_class, 'index_art_class');
			}
			if(!$this->view->dig_article = $cache->load('index_dig_article')) {
				load_model('Article');
				$article = new Article();
				$this->view->dig_article = $article->get_article_lists('order by art_count desc','limit 10');
			    $cache->save($this->view->dig_article, 'index_dig_article');
			}
			if(!$this->view->com_mes_ten = $cache->load('index_com_mes_ten')) {
				load_model('Comment');
				$comment = new Comment();
				load_model('Message');
				$message = new Message();
				//各取前十条评论留言
				$com_ten = $comment->get_tencom();
				$mes_ten = $message->get_tenmes();
				$this->view->com_mes_ten = twoarray_order($com_ten,'com_date',$mes_ten,'mes_date','desc',10);
			    $cache->save($this->view->com_mes_ten, 'index_com_mes_ten');
			}
			//页脚单页面
			if(!$this->view->sg_list = $cache->load('index_sg_list')) {
				load_model('SinglePage');
				$sp = new SinglePage();
				$this->view->sg_list = $sp->get_sp_list();
			    $cache->save($this->view->sg_list, 'index_sg_list');
			}
		}else{//前期不使用缓存，反而消耗资源
			load_model('Class');
			$class = new APclass();
			$this->view->header_class = $class->get_class_index();
			load_model('User');
			$user = new User();
			$this->view->user_info = $user->get_user_info(1);
			load_model('Class');
			$class = new APclass();
			$this->view->art_class = $class->get_artclass_list();
			load_model('Article');
			$article = new Article();
			$this->view->dig_article = $article->get_article_lists('order by art_count desc','limit 10');
			load_model('Comment');
			$comment = new Comment();
			load_model('Message');
			$message = new Message();
			//各取前十条评论留言
			$com_ten = $comment->get_tencom();
			$mes_ten = $message->get_tenmes();
			$this->view->com_mes_ten = twoarray_order($com_ten,'com_date',$mes_ten,'mes_date','desc',10);
			load_model('SinglePage');
			$sp = new SinglePage();
			$this->view->sg_list = $sp->get_sp_list();
			load_model('Photo');
			$pho = new Photo();
			$this->view->pho_list = $pho->get_photo_list();
			load_model('WriteMood');
			$wd= new WriteMood();
			$this->view->lastmood = $wd->get_lastest_mood();
		}
		//统计信息,修复缓存后改过来
		$this->view->article_total = count($article->get_article_lists());
		$this->view->photo_total = count($this->view->pho_list);
		$this->view->comment_total = count($comment->get_comment_list());
		$this->view->message_total = count($message->get_message_list());	
		//隐藏参数，清缓存用
		$clean_cache = (isset($_GET["clean_cache"]) && $_GET["clean_cache"] == true && $use_cache == true) ? true : false;	
		if($clean_cache){
			if(isset($_GET["clean_key"])){//删除指定key
				$cache->remove($_GET["clean_key"]);
			}else{//全部清除缓存
				$cache->clean(Zend_Cache::CLEANING_MODE_ALL);
			}
		}
    }
      
}
?>